# # coding=utf-8
import openpyxl
import xlrd
import time
import requests
import json
import datetime

excel_path = ('../../项目文件' )
path = 'D:\工作\python_easipass\项目文件\全流程项目场景录入.xlsx'

request_data = {}

ctrl_data ={"customCode":"2249","hitRate":"100","ctrlNo":"","ctrlNote":"1","startTime":"2022-12-14 10:39:53","endTime":"2023-12-31 10:39:53","rmk":"1","area":"L","sceneType":"1"}

class Excel_rpb():
    def swich_data(self, file_path):
        data = xlrd.open_workbook(file_path)
        table = data.sheet_by_index(0)

        # 查看工作表
        data.sheet_names()
        # print("全部sheet页如下：\n" + str(data.sheet_names()))
        # print("总行数：" + str(table.nrows))
        start_time = time.strftime('%Y-%m-%d %H:%M:%S')
        starttime = datetime.datetime.now()
        print("开始时间：" + str(start_time))

        for i in range(2, table.nrows):
            is_threshold = Excel_rpb().excel_get(table=table, i=i, j=2)
            is_swith = Excel_rpb().excel_get(table=table, i=i, j=3)
            if is_threshold != 0 or is_swith != 0:
                switchStatus = '0'
                SCENE_TYPE = Excel_rpb().excel_get(table=table, i=i, j=0)
                thresholdName = Excel_rpb().excel_get(table=table, i=i, j=1)
                thresholdDes = Excel_rpb().excel_get(table=table, i=i, j=6)

                if is_swith == 0:
                    switchStatus = '1'
                    thresholdDes = '(开关默认开启，无实际作用)\n' + thresholdDes

                if '时间' in thresholdName:
                    filterType = '0'
                    THRESHOLD_UNIT = '小时（H）'
                    thresholdSize = 1
                elif '重量' in thresholdName:
                    filterType = '1'
                    THRESHOLD_UNIT = '百分比（%）'
                    thresholdSize = 10
                elif '次数' in thresholdName:
                    filterType = '2'
                    THRESHOLD_UNIT = '次'
                    thresholdSize = 2
                else:
                    filterType = '3'
                    THRESHOLD_UNIT = ''
                    thresholdSize = ''

                request_data["filterType"] = filterType
                request_data["thresholdUnit"] = THRESHOLD_UNIT
                request_data["thresholdSize"] = thresholdSize
                request_data["switchStatus"] = switchStatus
                request_data["sceneType"] = SCENE_TYPE
                request_data["thresholdName"] = thresholdName
                request_data["thresholdDes"] = thresholdDes

                # print(request_data)
                response = Excel_rpb().post_requests(url='http://192.168.125.224:9069/v1/threshold/save',
                                                     data=json.dumps(request_data))
                print(response)
                time.sleep(0.5)

        endtime = datetime.datetime.now()
        use_time = (endtime-starttime).seconds

    def ctrl_data(self, file_path):
        data = xlrd.open_workbook(file_path)
        table = data.sheet_by_index(0)

        # 查看工作表
        data.sheet_names()
        # print("全部sheet页如下：\n" + str(data.sheet_names()))
        # print("总行数：" + str(table.nrows))
        start_time = time.strftime('%Y-%m-%d %H:%M:%S')
        starttime = datetime.datetime.now()
        print("开始时间：" + str(start_time))

        for i in range(2, table.nrows):
            is_ctrl = Excel_rpb().excel_get(table=table, i=i, j=4)
            if is_ctrl != 0:
                SCENE_TYPE = Excel_rpb().excel_get(table=table, i=i, j=0)
                thresholdName = Excel_rpb().excel_get(table=table, i=i, j=1)
                ctrlNote = Excel_rpb().excel_get(table=table, i=i, j=7)

                ctrl_data["sceneType"] = SCENE_TYPE
                ctrl_data["ctrlNote"] = ctrlNote
                ctrl_data["rmk"] = ctrlNote
                print(ctrl_data)
                response = Excel_rpb().post_requests('http://192.168.125.224:9069/ctrl/ctrlConfigSave',json.dumps(ctrl_data))
                print(response)
                time.sleep(0.5)

        endtime = datetime.datetime.now()
        use_time = (endtime-starttime).seconds
    def post_requests(self, url, data):
        #测试环境

        try:
            headers = {"Content-Type": "application/json","personId":"xhan","userName":"xhan"}
            response = requests.post(url=url, headers=headers, data=data)
            # print(data)
            ass_value = response.json()

            status = False
            return status, ass_value
        except Exception as E:
            return False, E, "请求异常"

    def excel_get(self, table, i, j):
        value = table.cell(i, j).value
        return value

if __name__ == "__main__":
    Excel_rpb().ctrl_data(path)
    # Excel_rpb().swich_data(path)